< previous page page_420 next page >

Page 420
Homogeneous Coordinates
As is the case in two dimensions, you can easily represent points and transformations in three dimensions using homogeneous coordinates. In two dimensions, you represent a point in as a vector [S * x, S * y, S], where x and y are the point's normal X and Y coordinates and S is a scale factor.
In three dimensions, you simply add another coordinate before the scaling factor. A typical point is represented as [S * x, S * y, S * z, S]. Here, x, y, and z are the point's coordinates in three-dimensional space, and S is a scale factor. For example, you could represent the point (1, 4, 2) as [10, 40, 20, 10] or as [0.5, 2, 1, 0.5]. As is the case in two-dimensional homogeneous coordinates, vectors are often normalized so that their scale factor is one. The normalized representation of the point (1, 4, 2) is [1, 4, 2, 1].
You can represent three-dimensional translation, scaling, and rotation as matrices. To apply one of these transformations to a point, you multiply the point's homogeneous vector representation by the corresponding transformation matrix. Just as you can in two dimensions, you can combine transformation matrices by multiplying them to represent complex combinations of translation, scaling, and rotation in a single matrix.
Vector and Matrix Operations in Visual Basic
As was the case in two dimensions, you can use the special structure of homogeneous coordinates and transformation matrices to speed up matrix operations. Most three-dimensional transformation matrices contain 0, 0, 0, 1 in their last columns. The following routine uses this fact to apply a transformation matrix to a point quickly. This routine also gains a slight speed benefit by performing its multiplications and additions directly without for loops.
' Apply a transformation matrix to a point.
Public Sub m3Apply(V() As Single, M() As Single, _
    Result() As Single)

    Result(1) = V(1) * M(1, 1) + _
                V(2) * M(2, 1) + _
                V(3) * M(3, 1) + M(4, 1)
    Result(2) = V(1) * M(1, 2) + _
                V(2) * M(2, 2) + _
                V(3) * M(3, 2) + M(4, 2)
    Result(3) = V(1) * M(1, 3) + _
                V(2) * M(2, 3) + _
                V(3) * M(3, 3) + M(4, 3)
    Result(4) = 1#
End Sub
Whenever you multiply two matrices containing 0, 0, 0, 1 in their final columns, you get another matrix with 0, 0, 0, 1 in the final column. The following subroutine uses this fact to make multiplying two matrices faster.

 
< previous page page_420 next page >